package com.youku.baseUI
{
   import flash.display.Sprite;
   import flash.display.Loader;
   import flash.system.LoaderContext;
   import flash.display.MovieClip;
   import flash.display.Bitmap;
   import flash.display.DisplayObject;
   import flash.display.LoaderInfo;
   import flash.net.URLRequest;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   
   public class BaseUILoader extends Sprite
   {
      
      public function BaseUILoader() {
         super();
         this._border = new Sprite();
         this.addChild(this._border);
         this._background = new Sprite();
         this.addChild(this._background);
      }
      
      private var _align:String = "center";
      
      private var _aspectRatio:Number;
      
      private var _autoLoad:Boolean = true;
      
      private var _background:Sprite;
      
      private var _backgroundColor:Number = 0;
      
      private var _border:Sprite;
      
      private var _borderColor:Number = 3355443;
      
      private var _borderSize:Number = 1;
      
      private var _configWidth:Number = 1;
      
      private var _configHeight:Number = 1;
      
      private var _dispatchErrorEvent:Boolean = true;
      
      private var _loader:Loader;
      
      private var _loaderContext:LoaderContext;
      
      private var _maintainAspectRatio:Boolean = true;
      
      private var _mask:Sprite;
      
      private var _scaleContent:Boolean = true;
      
      private var _showBackground:Boolean = true;
      
      private var _showBorder:Boolean = true;
      
      private var _source:String;
      
      private var _uiContainer:Sprite;
      
      private var _showLoading:Boolean = false;
      
      private var _loadingIcon:MovieClip = null;
      
      private var _image:Bitmap;
      
      private var _useBitmap:Boolean = false;
      
      public function get align() : String {
         return this._align;
      }
      
      public function set align(param1:String) : void {
         this._align = param1;
      }
      
      public function get aspectRatio() : Number {
         return this._aspectRatio;
      }
      
      public function set aspectRatio(param1:Number) : void {
         this._aspectRatio = param1;
      }
      
      public function get autoLoad() : Boolean {
         return this._autoLoad;
      }
      
      public function set autoLoad(param1:Boolean) : void {
         this._autoLoad = param1;
      }
      
      public function get backgroundColor() : Number {
         return this._backgroundColor;
      }
      
      public function set backgroundColor(param1:Number) : void {
         this._backgroundColor = param1;
      }
      
      public function get borderColor() : Number {
         return this._borderColor;
      }
      
      public function set borderColor(param1:Number) : void {
         this._borderColor = param1;
      }
      
      public function get content() : DisplayObject {
         if(this._loader)
         {
            try
            {
               return this._loader.content;
            }
            catch(e:*)
            {
               return _loader;
            }
            return null;
         }
         return null;
      }
      
      public function get contentLoaderInfo() : LoaderInfo {
         if(this._loader)
         {
            return this._loader.contentLoaderInfo;
         }
         return null;
      }
      
      public function get dispatchErrorEvent() : Boolean {
         return this._dispatchErrorEvent;
      }
      
      public function set dispatchErrorEvent(param1:Boolean) : void {
         this._dispatchErrorEvent = param1;
      }
      
      public function get hasValidLoaderContent() : Boolean {
         return !(this.content == null || (isNaN(this.content.width)) || (isNaN(this.content.height)) || this.content.width < 1 || this.content.height < 1);
      }
      
      override public function get height() : Number {
         return this._configHeight;
      }
      
      override public function set height(param1:Number) : void {
         this.setSize(this._configWidth,param1);
      }
      
      public function get loader() : Loader {
         return this._loader;
      }
      
      public function get loaderContext() : LoaderContext {
         return this._loaderContext;
      }
      
      public function set loaderContext(param1:LoaderContext) : void {
         this._loaderContext = param1;
      }
      
      public function get maintainAspectRatio() : Boolean {
         return this._maintainAspectRatio;
      }
      
      public function set maintainAspectRatio(param1:Boolean) : void {
         this._maintainAspectRatio = param1;
      }
      
      public function get scaleContent() : Boolean {
         return this._scaleContent;
      }
      
      public function set scaleContent(param1:Boolean) : void {
         this._scaleContent = param1;
      }
      
      public function get showBackground() : Boolean {
         return this._showBackground;
      }
      
      public function set showBackground(param1:Boolean) : void {
         this._showBackground = param1;
      }
      
      public function get showBorder() : Boolean {
         return this._showBorder;
      }
      
      public function set showBorder(param1:Boolean) : void {
         this._showBorder = param1;
      }
      
      public function get source() : String {
         return this._source;
      }
      
      public function set source(param1:String) : void {
         if(this._source == param1)
         {
            return;
         }
         this._source = param1;
         if((this._autoLoad) && !(this._source == null) && (this._source.indexOf("http") == 0 || this._source.indexOf("file") == 0))
         {
            this.load();
         }
      }
      
      public function get showLoading() : Boolean {
         return this._showLoading;
      }
      
      public function set showLoading(param1:Boolean) : void {
         this._showLoading = param1;
      }
      
      public function set loadingIcon(param1:MovieClip) : void {
         this._loadingIcon = param1;
      }
      
      public function get useBitmap() : Boolean {
         return this._useBitmap;
      }
      
      public function set useBitmap(param1:Boolean) : void {
         this._useBitmap = param1;
      }
      
      override public function get width() : Number {
         return this._configWidth;
      }
      
      override public function set width(param1:Number) : void {
         this.setSize(param1,this._configHeight);
      }
      
      public function close(param1:Boolean = false) : void {
         if(param1 == false)
         {
            this.clear();
         }
         if(this._loader == null)
         {
            return;
         }
         try
         {
            this._loader.close();
         }
         catch(e:*)
         {
         }
      }
      
      public function load(param1:URLRequest = null, param2:LoaderContext = null) : void {
         var lc:LoaderContext = null;
         var request:URLRequest = param1;
         var context:LoaderContext = param2;
         if(request != null)
         {
            if(request.url == this._source)
            {
               return;
            }
            this._source = request.url;
         }
         if(this._loader == null)
         {
            this._loader = new Loader();
            this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onUILoadComplete);
            this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onUILoadError);
            this._loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUILoadSecurityError);
            this._uiContainer = new Sprite();
            addChild(this._uiContainer);
            this._mask = new Sprite();
            this.addChild(this._mask);
            this.mask = this._mask;
            this._mask.graphics.beginFill(0);
            this._mask.graphics.drawRect(0,0,1,1);
            this._mask.graphics.endFill();
            this._uiContainer.mask = this._mask;
         }
         this.close(true);
         this.unload(true);
         var urlRequest:URLRequest = request || new URLRequest(this._source);
         this._loaderContext = context || this._loaderContext;
         try
         {
            if(this._loaderContext != null)
            {
               this._loaderContext.checkPolicyFile = true;
               this._loader.load(urlRequest,this._loaderContext);
            }
            else
            {
               lc = new LoaderContext(true);
               lc.checkPolicyFile = true;
               this._loader.load(urlRequest,lc);
            }
         }
         catch(e:Error)
         {
         }
      }
      
      private function onUILoadComplete(param1:Event) : void {
         var _loc2_:String = null;
         var _loc3_:LoaderContext = null;
         if(!this.hasValidLoaderContent)
         {
            _loc2_ = LoaderInfo(param1.target).url;
            if(_loc2_ != this._source)
            {
               _loc3_ = new LoaderContext(true);
               _loc3_.checkPolicyFile = true;
               this._loader.load(new URLRequest(_loc2_),_loc3_);
               return;
            }
            return;
         }
         if(this.useBitmap)
         {
            this.aspectRatio = this.content.width / this.content.height;
            if(this.content is Bitmap)
            {
               this._image = this.content as Bitmap;
               this._image.smoothing = true;
               this._uiContainer.addChild(this._image);
            }
         }
         else
         {
            this.aspectRatio = this.content.width / this.content.height;
            this._uiContainer.addChild(this._loader);
         }
         if(!this._scaleContent)
         {
            this._configWidth = this.content.width;
            this._configHeight = this.content.height;
         }
         this.setSize(this._configWidth,this._configHeight);
         dispatchEvent(param1.clone());
      }
      
      private function onUILoadSecurityError(param1:SecurityErrorEvent) : void {
         if(hasEventListener(param1.type))
         {
            dispatchEvent(param1.clone());
         }
      }
      
      private function onUILoadError(param1:IOErrorEvent) : void {
         if(hasEventListener(param1.type))
         {
            dispatchEvent(param1.clone());
         }
      }
      
      public function setSize(param1:Number, param2:Number) : void {
         this._configWidth = Math.round(param1);
         this._configHeight = Math.round(param2);
         if(this._showBorder == true)
         {
            this._border.visible = true;
            this._border.graphics.clear();
            this._border.graphics.beginFill(this._borderColor);
            this._border.graphics.drawRect(-this._borderSize,-this._borderSize,this._configWidth + 2 * this._borderSize,this._configHeight + 2 * this._borderSize);
            this._border.graphics.endFill();
         }
         else
         {
            this._border.visible = false;
         }
         this._background.graphics.clear();
         this._background.graphics.beginFill(this._backgroundColor);
         this._background.graphics.drawRect(0,0,this._configWidth,this._configHeight);
         this._background.graphics.endFill();
         this._background.visible = this._showBackground;
         if(this.hasValidLoaderContent == false)
         {
            return;
         }
         this._mask.width = this._configWidth;
         this._mask.height = this._configHeight;
         this._mask.x = 0;
         this._mask.y = 0;
         if(this._scaleContent)
         {
            if(this._maintainAspectRatio)
            {
               if(this._configWidth / this._configHeight < this.aspectRatio)
               {
                  this._uiContainer.width = this._configWidth;
                  this._uiContainer.height = this._configWidth / this.aspectRatio;
               }
               else
               {
                  this._uiContainer.height = this._configHeight;
                  this._uiContainer.width = this._configHeight * this.aspectRatio;
               }
            }
            else
            {
               this._uiContainer.width = this._configWidth;
               this._uiContainer.height = this._configHeight;
            }
         }
         else
         {
            this._uiContainer.scaleX = this._uiContainer.scaleY = 1;
         }
         switch(this.align)
         {
            case "topLeft":
               this._uiContainer.x = 0;
               this._uiContainer.y = 0;
               break;
            default:
               this._uiContainer.x = (this._configWidth - this._uiContainer.width) / 2;
               this._uiContainer.y = (this._configHeight - this._uiContainer.height) / 2;
         }
      }
      
      public function unload(param1:Boolean = false) : void {
         if(param1 == false)
         {
            this.clear();
         }
         while((this._uiContainer) && !(this._uiContainer.numChildren == 0))
         {
            this._uiContainer.removeChildAt(0);
         }
         this._image = null;
         if(this._loader == null)
         {
            return;
         }
         try
         {
            this._loader.unload();
         }
         catch(e:*)
         {
         }
      }
      
      private function clear() : void {
         this._source = null;
      }
   }
}
